<?php
// $Id$

/**
 * Controller_Admin_Userbindtel 控制器
 */
class Controller_Admin_Userbindtel extends Controller_Abstract
{
	function _format_appendSql()
	{
		if (isset($_SESSION['acct']))
		{
			if($_SESSION['acct']['acclevel'] == 3)
			{
				$append['acctname'] = $_SESSION['acct']['acctname'];
				$append['admin_id'] = $_SESSION['acct']['admin_id'];
				$append['agent_id'] = $_SESSION['acct']['agent_id'];
			}else {
				if ($_SESSION['acct']['admin_id'] == -1 && $_SESSION['acct']['agent_id'] == -1)
				{
				//为管理
					//$append['']
					$append['admin_id'] = $_SESSION['acct']['id'];
					//$append['agent_id'] = -1;
					//$append= array();
				}else
				{
					$append['admin_id'] = $_SESSION['acct']['admin_id'];
					$append['agent_id'] = Acct::_get_have_acctid($_SESSION['acct']['id'], $_SESSION['acct']['admin_id']);
			
				}
			}
			
		}
		return $append;
	}
	function actionIndex()
	{
        $this->union_table = Userbindtel::meta();
        $userid = $this->_context->_userid ;
        $user = Acctuser::find('id = ?',intval($userid))->getOne();
        
        $this->_view['user'] = $user;
        $param = $this->_format_appendSql();
        $param['acctname'] = $user->acctname;
        
        $this->lists($param);
	}
	function actionadd()
	{
		$this->union_table = Userbindtel::meta();
		//ini_set('allow_call_time_pass_reference', true); //开启引用？
		$handle = new helper_autoinsert($this->union_table);
		$form = new Form_UserbindTel_add(url($this->_context->controller_name.'/'.$this->_context->action_name));
		$userid = $_SESSION['acct']['acclevel'] == 3 ? $_SESSION['acct']['id'] : intval($this->_context->_userid);
		
		
		$acctname = Acctuser::find('id = ?',intval($userid))->getOne();
			if ($this->_context->isPOST())
			{
			//检测最大绑定数
				$maxBind = Acct::find('id = ?',$acctname->agent_id)->getOne();
				 $nowCount = Userbindtel::find('acctname = ?',$acctname->acctname)->getCount('id');
			 if(intval($nowCount) >= intval($maxBind->limitbindtel))
			 {
			 	$this->error('超出最大绑定数'.intval($maxBind->limitbindtel).',请联系代理！');
			 }
			}
		if (!$acctname->id())
		{
			$this->error('绑定的账号不存在');
		}else
		{
		//	unset($_GET['_userid']);
		}
		$result = $handle->add($form, 'id',$_GET);
		
		if ($this->_context->isGET())
		{
		//	$result->element('agent_id')->items = array_merge(array($_SESSION['acct']['id']=>'请选择一个代理'),Acct::_get_arrayinfo($this->_format_appendSql()));
			$result->element('acctname')->value = $acctname->acctname;
			$query = $this->_format_appendSql();
			$result->element('admin_id')->value = $acctname->admin_id;
			$result->element('agent_id')->value = $acctname->agent_id;
			$this->_view['form'] = $result;
			$this->_viewname = '../form/add';
		}else {
			
			Syslog::logs('编辑绑定电话:'.$this->_context->bindtel);
			return call_user_func_array(array($this,$handle->status),array($result));
		}
		
	}
	function actiondel()
		{
			Syslog::logs('删除绑定电话:'.$this->_context->ids);
			$this->union_table = Userbindtel::meta();
			$this->delete(array_merge($this->_format_appendSql(),array('id'=>$this->_context->ids)));
		}
}


